package com.augurit.smartform.web.parser;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.Struts2Utils;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.entity.SysCodeType;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.smartform.constant.FormConstant;
import com.augurit.smartform.database.DatabaseExecutor;
import com.augurit.smartform.database.SourceData;
import com.augurit.smartform.display.PageExecutor;
import com.augurit.smartform.service.ISmartFormService;
import com.augurit.smartform.service.IDesignerSupportService;
import com.augurit.smartform.test.TestUtils;


@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.SUCCESS, location = "/smartform/success.jsp")
})
public class DesignerSupportAction extends ExtBaseAction {
	@Autowired
	private IDesignerSupportService designerSupportService;
	@Autowired
	private ISysCodeService sysCodeService;
	
	
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 基于序列查询下一个控件的ID
	 */
	public void getNextFieldId(){
		BigDecimal result = designerSupportService.getNextFieldId();
		this.renderJson(result);
	}
	
	/**
	 * 基于序列查询下一个页面的ID
	 */
	public void getNextPageId(){
		BigDecimal result = designerSupportService.getNextPageId();
		this.renderJson(result);
	}
	
	/**
	 * 查询系统所有的数据字典
	 */
	public void getAllSysCodeTypes(){
		List<SysCodeType> result = sysCodeService.getAllCodeType();
		this.renderJson(result);
	}
	
	/**
	 * 查询系统所有表单数据
	 */
	public void getAllSmartForm(){
		List<Map> forms=designerSupportService.getAllParentForms();
		String allFormStr=Struts2Utils.getJsonString(forms);
		System.out.println("allFormStr="+allFormStr);
		this.renderJson(forms);
	}
	
	/**
	 * 查询表单的XML字符串
	 */
	public void getAllFieldByFormId(){
		String xmlStr="";
		String formId=this.getRequest().getParameter("formId");
		String sourceXmlStr=designerSupportService.getXmlStrByFormId(formId);
		if(StringUtils.isNotBlank(sourceXmlStr)){
			xmlStr=sourceXmlStr;
		}
		this.renderXml(xmlStr);
	}
	
	/**
	 * 通过模板ID加载相关的智能表单数据
	 */
	public void getSourceDataByTemplateId(){
		String templateId=this.getRequest().getParameter("templateId");
		List<Map> sourceDatas=designerSupportService.getSourceDataByTemplateId(templateId);
//		String allFormStr=Struts2Utils.getJsonString(sourceDatas);
//		System.out.println("sourceDatas="+sourceDatas);		
		this.renderJson(sourceDatas);
	}
	
	/**
	 * 通过页面ID加载相关的智能表单数据
	 */
	public void getSourceDataByPageId(){
		String pageId=this.getRequest().getParameter("pageId");
		List<Map> sourceDatas=designerSupportService.getSourceDataByPageId(pageId);
		this.renderJson(sourceDatas);
	}
	
	/**
	 * 得到所有父表单名称
	 * @return
	 * cuigeqiang
	 */
	public void getAllParentSmFormName(){
		List<Map> allFormName=designerSupportService.getAllParentSmFormName();
		String allFormNameStr=Struts2Utils.getJsonString(allFormName);
		System.out.println("allFormNameStr="+allFormNameStr);		
		this.renderJson(allFormName);		
	}
	
	/**
	 * 得到表单所有可用组件
	 */
	public void getAllComponent(){
		Map allComponent=designerSupportService.getAllAvailableComponent();
		String allComponentStr=Struts2Utils.getJsonString(allComponent);
		System.out.println("allComponentStr="+allComponentStr);		
		this.renderJson(allComponent);	
	}
}
